/**
 * @ author: 汪孟苹
 */
define(["jquery","bbcare","fu/app/comm/validate","fu/app/comm/attention",
	"fu/app/question/question.ui",
	"fu/app/question/question.event",
	"ac/app/cacheSpecialNurse.servers",
    "ac/app/cacheSpecialNurse.ui"],
    function ($,$bb,$validate,$attention,$questionUi,$questionEvent,$specialNurseServers,$specialNurseUi) {
    var event = {};

	/**
	 * 保存
	 */
	var onSave = function(){
		var preTime = $(this).attr("preTime");
    	var time = new Date().getTime();
		$(this).attr("preTime",time);
		if( (!preTime) || (preTime && time - preTime > 1000) ){
			if( $validate.init() ){
				var params = $(this).data("params");
				var save = $bb.getIocalStorage("cacheSave");
				var arrSave = save ? JSON.parse(save) : [];
				var paramsSave = $specialNurseServers.getQuestionParams(params);
				if( params.index == undefined ){ // 修改
					arrSave.push(paramsSave);
		        	$bb.setIocalStorage("cacheSave",JSON.stringify(arrSave));
		        	init(arrSave.length - 1);
		        }else{ // 新建
		        	arrSave.splice(params.index,1,paramsSave);
					$bb.setIocalStorage("cacheSave",JSON.stringify(arrSave));
					init(params.index);
		        }			
		    }
		}
	}

    /**
	 * 请求建档问卷成功后的回调函数
	 */
	var ajaxQueueArchiveContentCallBack = function(questiones,params){
		$questionUi.init({
        	id: "specialNurseContent",
        	status: "default",
            list: questiones,	  // 问卷
            values: params.values
        });
       	if(params.recordName) $("#specialNurseContent > form").find("input[name='Pub12x00010']").val(params.recordName);
        $questionEvent.init();
        // 校验之前提醒
        $attention.init();
        $('.validatebox').data("attentionFun",$attention.blurInit);
        $validate.blurInit();
        // 保存
        var topbars;
        topbars = [
			{
				"icon":"fa fa-floppy-o",
				"name":"保存",
				"callback":onSave,
				"params":params
			}
		];
		$bb.toolbar(topbars);
	}

	/**
	 * 点击任务card
	 */
	var onNavBarSelect = function(){
		$("#specialNurseTabBar li").removeClass("active");
		$(this).addClass("active").siblings().removeClass("active");
		var recordName = $(this).html();
		var templId = $(this).attr("templId");
		var seqNo = $(this).attr("seqNo");
		var taskId = $(this).attr("taskId");
		var taskName = $(this).html();
		var data = $specialNurseServers.getQuestions(templId,seqNo);
		var params = {
			"recordName":recordName, // 记录名称 新建有这个参数
			"taskId":taskId,
			"seqNo":seqNo,
			"taskName":taskName,
			"templId":templId
		}
		ajaxQueueArchiveContentCallBack(data.questiones,params);
	}

	/**
	 * specialNurse-task 渲染
	 */
	var specialTaskInit = function(data){
		$specialNurseUi.tabsInit({
			"id":"specialNurse-task",
			"curIndex":0,	// 默认进来是建档页
			"data":data,
		});
		$("#specialNurse-task .pageTab .btn-toolbar button").unbind("click");
		$("#specialNurse-task .pageTab .btn-toolbar button.btn-click").click(onNavBarSelect);
		$("#specialNurse-task .pageTab .btn-toolbar button.btn-click").first().click();
	}

	/**
	 * 删除保存在IocalStorage里的患者建档/门诊任务信息
	 */
	var delCacheSave = function(){
		var This = $(this).data();
		var index = $(This).index();
		var cacheSave = JSON.parse( $bb.getIocalStorage("cacheSave") );
		if( cacheSave.length > 0 ){
			var i = cacheSave.length - 1 - index;
			cacheSave.splice(i,1);
			$bb.setIocalStorage("cacheSave",JSON.stringify(cacheSave));
			init();
		}
	}

	/**
	 * 点击患者执行的事件
	 */
	var onPatientSelect = function(){
		$("#specialNurse-task button").removeClass("active");
		$(this).parent().addClass("active").siblings("li").removeClass("active");
		var index = $(this).parent().index();
		var cacheSave = JSON.parse( $bb.getIocalStorage("cacheSave") );
		if( cacheSave.length > 0 ){
			var i = cacheSave.length - 1 - index;
			var questionnaire = $specialNurseServers.getQuestions(cacheSave[i].info.templId,cacheSave[i].info.seqNo);
			var params = {
				"index":i,
				"taskId":cacheSave[i].info.taskId,
				"seqNo":cacheSave[i].info.seqNo,
				"taskName":cacheSave[i].info.taskName,
				"templId":cacheSave[i].info.templId,
				"values":cacheSave[i].params.questiones
			}
			ajaxQueueArchiveContentCallBack(questionnaire.questiones,params);
		}
	}

	/**
	 * 今日门诊
	 */
	var right = function(index){
		var cacheSave = JSON.parse( $bb.getIocalStorage("cacheSave") );
		$specialNurseUi.right({
    		id:"specialNurseTabBar",
    		list:cacheSave
    	});
    	$("#specialNurseTabBar li").mouseenter(function(){
    		var This = this;
    		$(this).children("div.text-right").css({"display":"block"});
    		$(this).children("div.text-right").unbind("click");
    		$(this).children("div.text-right").data(This);
    		$(this).children("div.text-right").click(delCacheSave);
    	}).mouseleave(function(){
    		$(this).children("div.text-right").css({"display":"none"});
    	});
    	$("#specialNurseTabBar li > div:first-child").unbind("click");
    	$("#specialNurseTabBar li > div:first-child").click(onPatientSelect);
    	if(index != undefined) {
    		var i = $("#specialNurseTabBar li").length - 1 -index;
    		$("#specialNurseTabBar li").eq(i).children("div:first-child").click();
    	}
	}

	/**
	 * 初始化入口
	 */
	var init = function(index){
		$("#space-free").html( $bb.getLocalStorageSpaceFree() );
		var resourceList = $specialNurseServers.getPageParams().resourceList;
		specialTaskInit(resourceList);
		right(index);
	}
	event.init = function(){
		init();
	}

    return event;
});